package com.hngates.project.service.impl;

import java.util.List;
import java.util.Objects;

import com.hngates.common.utils.DateUtils;
import com.hngates.common.utils.StringUtils;
import com.hngates.project.domain.BusinessException;
import com.hngates.project.domain.dto.SiteInformationDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hngates.project.mapper.SiteInformationMapper;
import com.hngates.project.domain.SiteInformation;
import com.hngates.project.service.ISiteInformationService;
import com.hngates.common.core.text.Convert;

/**
 * 工地信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-06-23
 */
@Service
public class SiteInformationServiceImpl implements ISiteInformationService {
    @Autowired
    private SiteInformationMapper siteInformationMapper;

    /**
     * 查询工地信息
     *
     * @param id 工地信息主键
     * @return 工地信息
     */
    @Override
    public SiteInformation selectSiteInformationById(Integer id) {
        return siteInformationMapper.selectSiteInformationById(id);
    }

    /**
     * 查询工地信息列表
     *
     * @param siteInformation 工地信息
     * @return 工地信息
     */
    @Override
    public List<SiteInformation> selectSiteInformationList(SiteInformation siteInformation) {
        return siteInformationMapper.selectSiteInformationList(siteInformation);
    }

    /**
     * 新增工地信息
     *
     * @param siteInformation 工地信息
     * @return 结果
     */
    @Override
    public int insertSiteInformation(SiteInformation siteInformation) {
        siteInformation.setCreateTime(DateUtils.getNowDate());
        return siteInformationMapper.insertSiteInformation(siteInformation);
    }

    /**
     * 修改工地信息
     *
     * @param siteInformation 工地信息
     * @return 结果
     */
    @Override
    public int updateSiteInformation(SiteInformation siteInformation) {
        siteInformation.setUpdateTime(DateUtils.getNowDate());
        return siteInformationMapper.updateSiteInformation(siteInformation);
    }

    /**
     * 批量删除工地信息
     *
     * @param ids 需要删除的工地信息主键
     * @return 结果
     */
    @Override
    public int deleteSiteInformationByIds(String ids) {
        return siteInformationMapper.deleteSiteInformationByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除工地信息信息
     *
     * @param id 工地信息主键
     * @return 结果
     */
    @Override
    public int deleteSiteInformationById(Integer id) {
        return siteInformationMapper.deleteSiteInformationById(id);
    }

    /**
     * @param informationList 工地信息列表
     * @param updateSupport   是否更新支持 如果已经存在 则进行更新数据
     * @param operName        操作用户
     * @return
     */
    @Override
    public String importInformation(List<SiteInformationDTO> informationList, Boolean updateSupport, String operName) {

        if (Objects.isNull(informationList) || informationList.size() == 0) {
            throw new BusinessException("导入工地信息不能为空");
        }

        StringBuilder importMsg = new StringBuilder();

        //循环 excel 中的记录
        for (SiteInformationDTO siteInformationdto : informationList) {

            try {



                //siteInformationdto 是循环中，当前的记录对象
                //根据当前excel中的对象去数据库中查，查询时，以Excel中名称作为参数去查，返回值是 与此项目同名的记录转为数据库对象
                SiteInformation siteInformation = siteInformationMapper.selectSiteInformationByProjectName(siteInformationdto.getProjectName());
                //判断数据库对象是否存在
                if (siteInformation == null) {
                    //如果不存在，则创建
                    siteInformation = new SiteInformation();
                    //给对象的字段名赋值
                    siteInformation.setProjectName(siteInformationdto.getProjectName());
                    siteInformation.setUnitId(siteInformationdto.getUnitId());
                    siteInformation.setLeader(siteInformationdto.getLeader());
                    siteInformation.setPhone(siteInformationdto.getPhone());
                    siteInformation.setProjectStatus(siteInformationdto.getProjectStatus());
                    siteInformation.setAccessNumber(siteInformationdto.getAccessNumber());
                    siteInformation.setRiskGrade(siteInformationdto.getRiskGrade());

                    //赋值完成后，将这个对象插入到数据库中：
                    siteInformationMapper.insertSiteInformation(siteInformation);

                    importMsg.append("<br/>" + "工地信息" + siteInformation.getProjectName() + "导入成功");

                } else if (updateSupport) {
                    siteInformation.setUpdateBy(operName);
                    siteInformation.setUnitId(siteInformationdto.getUnitId());
                    siteInformation.setLeader(siteInformationdto.getLeader());
                    siteInformation.setPhone(siteInformationdto.getPhone());
                    siteInformation.setProjectStatus(siteInformationdto.getProjectStatus());
                    siteInformation.setAccessNumber(siteInformationdto.getAccessNumber());
                    siteInformation.setRiskGrade(siteInformationdto.getRiskGrade());
                    siteInformationMapper.updateSiteInformation(siteInformation);
                    importMsg.append("<br/>" + "工地信息" + siteInformation.getProjectName() + "更新成功");

                } else {
                    importMsg.append("<br/>" + "工地信息" + siteInformation.getProjectName() + "已存在");
                }

            } catch (Exception e) {
                importMsg.append("<br/>" + "工地信息" + siteInformationdto.getProjectName() + "导入时异常");

            }

        }
        return importMsg.toString();
    }


}
